<template>
	<view class="registered-shop">
		<!-- 广告位图片 -->
		<image style="width: 100%; height: 300upx;" :src="advertisementImg" mode=""></image>
		<view class="registered-shop-content">
			<!-- 手机号输入框 -->
			<view class="registered-shop-phone">
				<view class="">
					手机号：
				</view>
				<input type="text" value="" placeholder="请输入手机号码" v-model="userPhone"/>
			</view>
			<!-- 验证码输入框 -->
			<view class="registered-shop-phone" style="margin-top: 50upx;">
				<view class="">
					验证码：
				</view>
				<input type="text" placeholder="请输入验证码" v-model="authCode" style="width: 250upx;"/>
				<view class="registered-shop-phone-btn" v-if="show_Code" @click="CodeClick">
					获取验证码
				</view>
				<view class="registered-shop-phone-btn" v-if="!show_Code">{{count}} s</view>
			</view>
			<view class="registered-shop-btn" @click="registered">
				立即注册开店
			</view>
			<view class="registered-shop-footer">
				点击注册开店即代表同意
				<view class="" style="display: inline-block;" @click="protocolPage(protocolList[0])">
					《用户协议》
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {formatFullTime} from '@/common/util.js'
	import { testLogin, userLogin, newInfo, login } from '@/common/login.js';
	export default {
		data() {
			return {
				// 广告位图片
				advertisementImg:'',
				// 手机号
				userPhone:'',
				// 验证码
				authCode:'',
				// 验证码类型
				code_type:'register',
				show_Code:true,
				timer:'',
				count:'',
				protocolList:[],
				page:'',
				shopShare:0,
				customer_store_id:0,
				currentPge:'',
			}
		},
		onLoad(options) {
			// if(options.source){
			// 	location.href = this.$store.state.URL + '/customer/user/wx_login?page=/pages/memberCenter/personPerformance/signupInfo?store_id='+options.store_id+'&order_id='+options.order_id;
			// }
			if( options.shopShare == '1' ){
				this.$store.commit('updataShare', 1)
				this.shopShare = 1
			}
			
			console.log(options,"???????????????????????????????");
			this.page = options.page
			// #ifdef H5
				this.page = uni.getStorageSync("vip_page")
			// #endif
			if(options.token){
				this.token = options.token
				uni.setStorageSync('token', options.token);
				this.$store.commit('updateToken',options.token)
			}else if( options.share_id ){
				// #ifdef MP-WEIXIN
					let share_id = parseInt(options.share_id,10);
					let store_id = parseInt(options.store_id,10);
					userLogin(share_id,store_id).then((res)=>{
						console.log(res)
						console.log(555555)
						this.gainShopId()
					});
				// #endif
			}
			// #ifdef H5
			let pages = getCurrentPages()
			this.currentPge = pages[0].$route.fullPath
			if(!this.$store.getters.getToken){
				let url = this.$store.state.URL + "/customer/user/wx_login?page=" + encodeURIComponent(this.currentPge)
				location.href = url
			}
			// #endif
			
			this.getprotocol()
			this.getAdvertisementImg()
			this.gainShopId()
		},
		methods: {
			// 获取验证码
			CodeClick(){
				let that = this;
				if (!((/^1(3|4|5|6|7|8|9)\d{9}$/).test(this.userPhone))) {
					uni.showToast({
						title: "手机号有误，请重新输入!",
						duration: 2000,
						icon: "none"
					})
				} else {
					this.checkCode();
					let data = {
						mobile:this.userPhone,
						code_type:this.code_type,
						token:this.token
					}
					this.$api.gainAuthCode(data).then(res => {
						console.log(res)
						if(res.status){
							uni.showToast({
								title: res.msg,
								icon:"none",
								duration:2000
							});
						}else{
							uni.showToast({
								title: res.msg,
								icon:"none",
								duration:2000
							});
							this.show_Code = false;
						}
					}).catch(err => {
						console.log(res)
					})
				}
			},
			//验证码30s函数
			checkCode() {
				const TIME_COUNT = 60;
				if (!this.timer) {
					this.count = TIME_COUNT;
					this.show_Code = false;
					this.timer = setInterval(() => {
						if (this.count > 0 && this.count <= TIME_COUNT) {
							this.count--;
						} else {
							this.show_Code = true;
							clearInterval(this.timer);
							this.timer = null;
						}
					}, 1000)
				} else {
					uni.showToast({
						title: "手机号不得为空！",
						duration: 2000,
						icon: "none"
					})
				}
			},
			// 用户协议
			getprotocol(){
				
				let data = {
					token:this.$store.getters.getToken,
					type:'banksetting'
				}
				this.$api.bingind_protocol(data).then(res => {
					console.log(res)
					if(res.status){
						this.protocolList = res.list.data
					}
				}).catch(err => {
					console.log(err)
				})
			},
			protocolPage(item){
				uni.navigateTo({
					url:'userAgreement?store_id=' + this.store_id
				})
			},
			gainShopId(){
				console.log('gainShopId',this.token)
				let data = {
					token:this.$store.getters.getToken,
				}
				console.log(data,'gainShopId data')
				this.$api.judge_store(data).then(res => {
					console.log(res,'judge_store')
					if(res.status){
						// this.$store.commit('updataOrganId',res.customer_organization_id);
						this.$store.commit('updateStoreId',res.store_id);
						// uni.setStorageSync("OrganId",res.customer_organization_id)
						uni.setStorageSync("shopID",res.store_id)
						console.log(this.$store.getters.getStoreId,'this.$store.getters.getShopId')
						if(this.shopShare == 1){
							this.customer_store_id = res.customer_store_id
							this.addUserStore()
						}else{
							if(this.$store.getters.getStoreId){
								if( this.page == 'share' ){
									uni.setStorageSync("share_store_id",res.customer_store_id)
									uni.redirectTo({
										// url:'/pages/memberCenter/vip/vip?page=share&store_id='+res.customer_store_id
										url:'/pages/memberCenter/memberCenter?store_id='+this.$store.getters.getStoreId
									})
								}else{
									uni.redirectTo({
										url:'/pages/recommend/recommend?store_id=' + this.$store.getters.getStoreId
									})
								}
							}
						}
					}
				}).catch(err => {
					console.log(err,'this.$api.judge_store')
				})
			},
			addUserStore(){
				// #ifdef H5
				let data = {
					token:this.$store.getters.getToken,
					store_id: this.$store.getters.getStoreId,
					type:2,
				}
				// #endif
				// #ifdef MP-WEIXIN
				let data = {
					token:this.$store.getters.getToken,
					store_id: this.$store.getters.getStoreId,
					type:1,
				}
				// #endif
				console.log("add_user_store",data);
				// 3.发送请求
				this.$api.add_user_store(data).then(res => {
					console.log("add_user_store",res);
					if(res.status){
						if(this.$store.getters.getStoreId){
							if( this.page == 'share' ){
								uni.setStorageSync("share_store_id",this.customer_store_id)
								uni.redirectTo({
									// url:'/pages/memberCenter/vip/vip?page=share&store_id='+this.customer_store_id
									url:'/pages/memberCenter/memberCenter?store_id='+this.$store.getters.getStoreId
								})
							}else{
								uni.redirectTo({
									url:'/pages/recommend/recommend?store_id=' + this.$store.getters.getStoreId
								})
							}
						}
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none',
							duration:2000
						})
					}
				}).catch(err =>{
					console.log(err)
				})
			},
			// 广告位图片
			getAdvertisementImg(){
				let data = {
					token:this.$store.getters.getToken,
				}
				this.$api.get_advertisementImg(data).then(res => {
					console.log(res)
					if(res.status){
						this.advertisementImg = res.data.head_img
					}
				}).catch(err => {
					console.log(err)
				})
			},
			
			// 注册店铺
			registered(){
				if(this.userPhone == ''){
					uni.showToast({
						title:"请输入电话号码!",
						icon:"none",
						duration:2000
					})
					return false
				}
				
				if(this.authCode == ''){
					uni.showToast({
						title:"请输入验证码!",
						icon:"none",
						duration:2000
					})
					return false
				}
				let time = formatFullTime(new Date().getTime())
				let data = {
					token:this.$store.getters.getToken,
					customer_id:this.$store.getters.getCustomerId,
					code:this.authCode,
					telephone:this.userPhone,
					date_added:time,
					status:'1',
				}
				console.log(data,"+++++++++++++++++++++++++++++++++++++++++++++++++++++++");
				this.$api.organizationRegister(data).then(res => {
					console.log(res)
					if(res.status){
						uni.showToast({
							title:'机构注册成功',
							icon:'none',
							duration:2000
						})
						this.$store.commit('updataOrganId',res.customer_organization_id);
						this.$store.commit('updateStoreId',res.customer_store_id);
						uni.setStorageSync("OrganId",res.customer_organization_id)
						uni.setStorageSync("store_id",res.customer_store_id)
						if( this.page == 'share' ){
							uni.setStorageSync("share_store_id",res.customer_store_id)
							setTimeout(()=> {
								uni.redirectTo({
									// url:'/pages/memberCenter/vip/vip?page=share&store_id='+res.customer_store_id
									url:'/pages/memberCenter/memberCenter?store_id='+res.customer_store_id
								})
							},1000)
						}else{
							setTimeout(()=> {
								uni.redirectTo({
									url:'/pages/memberCenter/shop/shopMsgAmend/shopMsgAmend?store_id='+res.customer_store_id
								})
							},1000)
						}
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none',
							duration:2000
						})
					}
				}).catch(err => {
					console.log(err)
				})
				// uni.switchTab({
				// 	url:'../home'
				// })
			}
		}
	}
</script>

<style>
.registered-shop{
	height: 100vh;
}
.registered-shop-content{
	padding: 60upx;
}
.registered-shop-phone{
	font-size: 34upx;
	padding-bottom: 20upx;
	border-bottom: 1upx solid #D8D8D8;
}
.registered-shop-phone>view{
	display: inline-block;
	vertical-align: middle;
	margin-right: 20upx;
}
.registered-shop-phone>input{
	display: inline-block;
	vertical-align: middle;
}
.registered-shop-phone-btn{
	position: absolute; 
	right: 60upx;
	color: #55A0F0;
	font-size: 32upx;
	margin-top: 5upx;
	vertical-align: middle;
}
.registered-shop-btn{
	padding: 16upx;
	font-size: 34upx;
	margin-top: 64upx;
	text-align: center;
	border-radius: 60upx;
	color: #ffffff;
	font-weight: bold;
	background-image: linear-gradient(90deg, #FE9543 14%, #FF664F 100%);
}
.registered-shop-footer{
	font-size: 24upx; 
	margin-top: 64upx;
	text-align: center;
	color: rgba(0,0,0,0.85);
}
.registered-shop-footer>view{
	color: #55A0F0;
}
</style>
